<?php

namespace app\admin\model\mental;

use think\Model;
use app\admin\model\mental\ResultItem;

class Result extends Model
{
    // 表名
    protected $name = 'mental_result';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    /**
     * Undocumented function
     * 保存数据
     * @return void
     */
    public function saveData($data, $main_id, $component_id = 0,$type=0)
    {
        $item = $this->where('main_id', $main_id)->where('component_id',$component_id)->find();
        if (empty($item)) {
            $this->save(['main_id' => $main_id, 'content' => $data,'component_id'=>$component_id,'type'=>$type]);
            $result_id = $this->id;
        } else {
            $result_id = $item->id;
            $item->component_id = $component_id;
            $item->type = $type;
            $item->content = $data;
            $item->save();
        }
        $resultItem = new ResultItem();
        return $resultItem->saveData($data, $main_id, $result_id,$component_id);
    }

    public function setContentAttr($value)
    {
        return json_encode($value, JSON_UNESCAPED_UNICODE);
    }
}
